package com.bsj.power.job.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.def.dto.PageDTO;
import com.bsj.power.common.def.dto.job.ExperienceDTO;
import com.bsj.power.common.def.entity.exp.Experience;
import com.bsj.power.common.file.web.MediaFileUtil;
import com.bsj.power.common.mapper.ExperienceMapper;
import com.bsj.power.job.service.ExperienceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 经验
 * @date 2024/5/28
 */
@Service
@Slf4j
public class ExperienceServiceImpl implements ExperienceService {

    @Resource
    private ExperienceMapper experienceMapper;

    @Value("${file.exp.feedbackPath}")
    private String feedbackPath;

    /**
     * 新增修改经验
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addAndUpdate(ExperienceDTO vo) throws Exception{
        Long experienceId = vo.getExperienceId();
        Experience experience = new Experience();
        BeanUtil.copyProperties(vo, experience);
        MultipartFile[] files = vo.getFiles();
        if (experienceId == null) {
            //新增
            //文件上传
            String annexUrl = MediaFileUtil.upload(files, feedbackPath);
            if (annexUrl != null) {
                experience.setAnnexUrl(annexUrl);
            }
            experienceMapper.insert(experience);
        } else {
            //修改
            if (files != null) {
                Experience exp = experienceMapper.selectById(experienceId);
                String expAnnexUrl = exp.getAnnexUrl();
                //先删除文件
                MediaFileUtil.delete(expAnnexUrl);
                //文件上传
                String annexUrl = MediaFileUtil.upload(files, feedbackPath);
                if (annexUrl != null) {
                    experience.setAnnexUrl(annexUrl);
                }
            }
            experienceMapper.updateById(experience);
        }
        return JsonResult.success();
    }


    /**
     * 获取经验反馈列表
     * @param dto
     * @return
     */
    @Override
    public JsonResult<List<Experience>> pageList(PageDTO dto) {
        IPage<Experience> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        QueryWrapper<Experience> queryWrapper = new QueryWrapper<>();
        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            queryWrapper.lambda().between(Experience::getCreateTime, dto.getStartTime(), dto.getEndTime());
        }
        queryWrapper.lambda().orderByDesc(Experience::getCreateTime);
        IPage<Experience> iPage = experienceMapper.selectPage(page, queryWrapper);
        List<Experience> records = iPage.getRecords();
        return JsonResult.success(records, iPage.getTotal());
    }

    /**
     * 根据ID获取详情
     * @param experienceId
     * @return
     */
    @Override
    public JsonResult<Experience> findIdByInfo(Long experienceId) {
        Experience experience = experienceMapper.selectById(experienceId);
        return JsonResult.success(experience);
    }

    /**
     * 批量删除经验反馈
     * @param experienceIds
     * @return
     */
    @Override
    public JsonResult<Integer> deleteByIds(Long[] experienceIds) {
        int batchIds = experienceMapper.deleteBatchIds(Arrays.asList(experienceIds));
        return JsonResult.success(batchIds);
    }
}
